let heredoc = require('heredoc');
let crypto = require('crypto');

let createNonce = function () {
  return Math.random().toString(36).substr(2, 15)
};
let createTimeStamp = function () {
  return parseInt(new Date().getTime() / 1000, 10) + ''
};
function _sign(nonceStr, ticket, timeStamp ,url) {
  let params = [
    'noncestr=' + nonceStr,
    'jsapi_ticket=' + ticket,
    'timestamp=' + timeStamp,
    'url=' + url
  ];
  let str = params.sort().join('&');
  console.log(str)
  let shasum = crypto.createHash('sha1');

  shasum.update(str);

  return shasum.digest('hex')
}
exports.sign = function(ticket, url) {
  let nonceStr = createNonce();
  let timeStamp = createTimeStamp();
  let signature = _sign(nonceStr, ticket, timeStamp ,url);
  return {
    noncestr: nonceStr,
    timestamp: timeStamp,
    signature: signature
  }
}

exports.movie = heredoc(function () {
  /*
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>movie</title>
</head>
<body>
<div id="app">
    <h1>点击开始录音</h1>
</div>
<!-- built files will be auto injected -->
<script src="https://cdn.bootcss.com/zepto/1.1.6/zepto.min.js"></script>
<script src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
<script>
  wx.config({
    debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
    appId: 'wxb42f8f9d32c3da4e', // 必填，公众号的唯一标识
    timestamp: '<%= timestamp %>', // 必填，生成签名的时间戳
    nonceStr: '<%= noncestr %>', // 必填，生成签名的随机串
    signature: '<%= signature %>',// 必填，签名
    jsApiList: [
      'startRecord',
      'stopRecord',
      'onVoiceRecordEnd',
      'translateVoice',
    ] // 必填，需要使用的JS接口列表
  });
  wx.error(function(res){
    console.log(res)
    // config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
  });
  wx.ready(function(){
  console.log('success')
    wx.checkJsApi({
      jsApiList: ['startRecord','stopRecord','onVoiceRecordEnd','translateVoice'],
      success: function(res) {
        console.log(res)
      }
    });

    let isRecording = false;
    $('h1').on('click', function(){
    alert('haha')
      if (!isRecording) {
        isRecording = true;
        wx.startRecord({
          cancel: function(){
            alert('那就不能搜索了')
          }
        })
        return
      }

      isRecording = false;
      wx.stopRecord({
        complete: function (res) {
          console.log('stop')
        },
        success: function (res) {
          let localId = res.localId;

          wx.translateVoice({
            localId: localId,
            isShowProgressTips: 1,
            success: function (res) {
              alert(res.translateResult); // 语音识别的结果
            }
          });
        }
      });
    });


  });
</script>
</body>
</html>

*/
});
